import {reactive } from "vue";
import Mock from 'mockjs'




var projectManage_list = {};

var projectManage_searchlist = reactive([]);

if (localStorage.getItem('projectManage_list')) {
    projectManage_list = JSON.parse(localStorage.getItem('projectManage_list'))
} else {
    projectManage_list = Mock.mock({
        'list|100': [
            {
                "projectNum|+1": 10001,
                projectName: '项目一',
                projectManager: '@cname',
                projectdescribe: '暂未添加描述',
                technicaler: '@cname',
                product: '@cname',
                tester: '@cname',
                Marketer: '@cname',
                projectStage: '需求对接',
                projectState: '进行中',
                startDate: '@date()',
                endDate: '@date()'
            }
        ]
    })
    localStorage.setItem('projectManage_list', JSON.stringify(projectManage_list))
}

Mock.mock('/api/projectManage/list', 'get', () => {
    //返回数据
    return projectManage_list;
})

//详情数据
Mock.mock('/api/projectManage/info', 'post', (req) => {
    var { projectNum } = JSON.parse(req.body);
    var i = projectManage_list.list.findIndex((item) => {
        return item.projectNum == projectNum;
    })
    if (i != -1) {
        // console.log(projectManage_list.list[i]);
        return projectManage_list.list[i];
    }

})

// 删除数据

Mock.mock('/api/projectManage/delete', 'post', (req) => {
    var { projectNum } = JSON.parse(req.body);
    var i = projectManage_list.list.findIndex((item) => {
        return item.projectNum == projectNum;
    })
    if (i != -1) {
        projectManage_list.list.splice(i, 1);
        localStorage.setItem('projectManage_list', JSON.stringify(projectManage_list))
    }
    return { status: 200, msg: '删除成功!', list: projectManage_list.list }
})


// 编辑数据
Mock.mock('/api/projectManage/edit', 'post', (req) => {
    var y = JSON.parse(req.body);
    var x = JSON.parse(JSON.stringify(y.form));
    var projectNum = x._value.projectNum
    var i = projectManage_list.list.findIndex((item) => {
        return item.projectNum == projectNum;
    })
    if (i != -1) {
        console.log(x._value);
        projectManage_list.list[i] = x._value;
        localStorage.setItem('projectManage_list', JSON.stringify(projectManage_list))
    }
    return { status: 200, msg: '修改成功!', list: projectManage_list.list }
})

//新增数据
Mock.mock('/api/projectManage/add', 'post', (req) => {
    var y = JSON.parse(req.body);
    var x = JSON.parse(JSON.stringify(y.form));
    var projectNum = x._value.projectNum
    var i = projectManage_list.list.findIndex((item) => {
        return item.projectNum == projectNum;
    })
    if (i == -1) {
        projectManage_list.list.shift(x._value) ;
        localStorage.setItem('projectManage_list', JSON.stringify(projectManage_list))
    }else{
        return { status: 400, msg: '修改失败，项目已存在！'}
    }
    return { status: 200, msg: '修改成功!', list: projectManage_list.list }
})

// 搜索数据
Mock.mock('/api/projectManage/search', 'post', (req) => {
    projectManage_searchlist = []
    var  projectState  = JSON.parse(req.body).key;
    if (projectState == "所有"){
        return { status: 200, msg: '查询成功!', list: projectManage_list.list  };
    }
    var n = projectManage_list.list.length;
    for(let i=0; i<n; i++){
        if (projectState == projectManage_list.list[i].projectState) {
            projectManage_searchlist.push(projectManage_list.list[i])
        }
    }

    return { status: 200, msg: '查询成功!', list: projectManage_searchlist } ;
})